<?php

namespace app\common\model;

use think\Model;

class Culverts extends Model
{
    public function add_one($info){
        $insert['display'] = 1;
        $insert['inputtime'] = time();
        $insert['culvertNo'] = $info['culvertNo'];
        $insert['centerDistance'] = $info['centerDistance'];
        $insert['address'] = $info['address'];
        $insert['longitude'] = $info['longitude'];
        $insert['latitude'] = $info['latitude'];
        $insert['insId'] = $info['insId'];
        return $this->insertGetId($insert);
    }

     function getList($page,$pageSize,$search,$order = [],$field='*'){
        $currentPage = $page;
        $page = ($page - 1) * $pageSize;
        $where = ['insId'];
        $info = self::field($field)->withSearch($where,$search )->where(['display'=>1])->order($order)->limit($page,$pageSize)->select();
        $count = self::withSearch($where,$search )->where(['display'=>1])->count();
        foreach ($info as $infoKey => $infoValue){
            $info[$infoKey]['inputtime'] = date('Y-m-d H:i',$infoValue['inputtime']);
        }
        $arr =  ['list'=>$info,'count'=>$count,'page'=>$currentPage,'pageSize'=>$pageSize];
        return $arr;
    }

    public function get_one($id,$field='*'){
        return $this->field($field)->where(['id'=>$id,'display'=>1])->find();
    }

    public function edit($id,$info){
        $update['culvertNo'] = $info['culvertNo'];
        $update['centerDistance'] = $info['centerDistance'];
        $update['address'] = $info['address'];
        $update['longitude'] = $info['longitude'];
        $update['latitude'] = $info['latitude'];
        $update['insId'] = $info['insId'];

        return $this->where(['id'=>$id,'display'=>1])->update($update);
    }

    public function searchInsIdAttr($query,$value, $data)
    {
        if($value){
            $query->where('insId','=', $value);
        }

    }

    public function del($id){
        return $this->where(['id'=>$id,'display'=>1])->update(['display'=>0]);
    }

    public function get_arr(){
        return $this->field(['id','culvertNo'])->where(['display'=>1])->select();
    }
}
